home *** CD-ROM | disk | FTP | other *** search
- /*
- This file was used to convert "Image" palettes into CFG palettes.
- */
-
-
- #include <string.h>
-
-
- typedef struct identifier_struct // 128 byte header
- {
- char zero[4]; // first 4 bytes must be 0
- char appl_name[32]; // application name
- char by[32];
- char file_type[8]; // fractal, palette, cusfrac, etc.
- char version[4]; // application version: x.xx
- char earliest_vers[4]; // earliest app version that can open file
- char registered[4];
- char password[8];
- char reserved[32];
- } identifier_struct;
-
- typedef struct file_palette_header
- {
- long entries; // number of entries in palette
- Str255 load_pal_name; // name of palette
- } file_palette_header;
-
- typedef struct image_palette
- {
- short entries;
- unsigned char red[32];
- unsigned char green[32];
- unsigned char blue[32];
- RGBColor color[240];
- } image_palette;
-
-
- static image_palette pal;
- static char file_cmp[4] = { '2', '.', '0', '0' };
- static char file_erl[4] = { '2', '.', '0', '0' };
-
-
- #define FILE_ID_PALETTE "palette"
- #define FILE_APPL_NAME "Color Fractal Generator"
- #define FILE_BY "John A. Schlack Shadowbane"
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void main( void );
- Boolean GetPaletteFromFile( char * fname, short vnum );
- void _pstrcpy( unsigned char * dest, unsigned char * src );
- void pstrcat( unsigned char * dest, unsigned char * src );
- void WritePaletteToFile( char * name, short vnum );
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void main( void )
- {
- Point thePoint = {-1, -1};
- SFReply reply;
- SFTypeList typeList;
- char name[64];
-
- InitGraf( &thePort );
- InitFonts();
- FlushEvents( everyEvent, 0 );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
-
- typeList[0] = 'ICOL';
- for (;;)
- {
- SFGetFile( thePoint, 0L, 0L, 1, typeList, 0L, &reply );
- if (!(reply.good)) break;
- _pstrcpy( (unsigned char *) name, reply.fName );
- pstrcat( (unsigned char *) name, "\p.cfg" );
- if (GetPaletteFromFile( (char *) (reply.fName), reply.vRefNum ))
- WritePaletteToFile( name, reply.vRefNum );
- }
- }
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- Boolean GetPaletteFromFile( char * fname, short vnum )
- {
- short f;
- long size;
- Boolean res;
- char head[32];
- OSErr theErr;
-
- if (FSOpen( fname, vnum, &f ) != noErr)
- return false;
- size = 32L;
- if (FSRead( f, &size, head ) != noErr)
- {
- FSClose( f );
- return false;
- }
- pal.entries = (short) (head[0]);
- size = 32L;
- theErr = FSRead( f, &size, pal.red );
- size = 32L;
- theErr = FSRead( f, &size, pal.green );
- size = 32L;
- theErr = FSRead( f, &size, pal.blue );
- FSClose( f );
- return true;
- }
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void _pstrcpy( dest, src )
-
- unsigned char * dest;
- unsigned char * src;
-
- {
- asm {
- movea.l dest,a1 ; use movea instead of lea since dest and src are
- movea.l src,a0 ; already addresses
- clr.w d0 ; clear low word of d0 for loop constant
- move.b (a0),d0 ; want to copy *src + 1 bytes
- @loop move.b (a0)+,(a1)+ ; copy a byte
- dbra d0,@loop
- }
- }
-
-
- /* -------------------------------------------------------------------------------- */
-
-
- void pstrcat( dest, src )
-
- unsigned char * dest;
- unsigned char * src;
-
- {
- unsigned char * q = dest + (*dest) + 1; // move to open space
- short cnt, i;
-
- if ((*dest + *src) > 255)
- cnt = 255 - *dest;
- else
- cnt = *src;
-
- src++; // move beyond size
- for (i=0; i<cnt; i++)
- *q++ = *src++;
- (*dest) += cnt;
- }
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void WritePaletteToFile( char * name, short vnum )
- {
- identifier_struct ID;
- file_palette_header pal_head;
- OSErr theErr;
- short f, start, i, first;
- long size;
- char r, g, b;
- double f1, f2;
-
- theErr = Create( name, vnum, 'fraG', 'fraP');
- if ((theErr != noErr) && (theErr != dupFNErr))
- return;
-
- theErr = FSOpen( name, vnum, &f );
- if (theErr != noErr)
- {
- FSDelete( name, vnum );
- return;
- }
-
- r = pal.red[0]; g = pal.green[0]; b = pal.blue[0];
- for (i=1, start=0; i<pal.entries; i++)
- {
- if ((r == pal.red[i]) && (g == pal.green[i]) && (b == pal.blue[i]))
- start++;
- else
- break;
- }
-
- if (i >= pal.entries)
- {
- FSClose( f );
- FSDelete( name, vnum );
- return;
- }
-
- for (i=0; i<240; i++)
- {
- f1 = (double) (pal.entries - start - 1) * i / 239.0 + (double) start;
- first = (short) f1;
- f1 = 1.0 - (f1 - (double) first);
- f2 = 1.0 - f1;
- pal.color[i].red = ((unsigned short) ((double) pal.red[first] * f1 +
- (double) pal.red[first+1] * f2)) << 8;
- pal.color[i].green = ((unsigned short) ((double) pal.green[first] * f1 +
- (double) pal.green[first+1] * f2)) << 8;
- pal.color[i].blue = ((unsigned short) ((double) pal.blue[first] * f1 +
- (double) pal.blue[first+1] * f2)) << 8;
- }
-
- strcpy( ID.appl_name, FILE_APPL_NAME );
- strcpy( ID.by, FILE_BY );
- strcpy( ID.file_type, FILE_ID_PALETTE );
- memcpy( ID.version, file_cmp, 4L );
- memcpy( ID.earliest_vers, file_erl, 4L );
-
- pal_head.entries = 240L;
- _pstrcpy( pal_head.load_pal_name, (unsigned char *) name );
-
- // WRITE ID & PALETTE
-
- size = (long) sizeof( identifier_struct );
- FSWrite( f, &size, &ID );
- size = (long) sizeof( file_palette_header );
- FSWrite( f, &size, &pal_head );
- for (i=0; i<240; i++)
- {
- size = (long) sizeof( RGBColor );
- FSWrite( f, &size, &(pal.color[i]) );
- }
-
- // CLEAN UP
-
- GetFPos( f, &size );
- SetEOF( f, size );
- FSClose( f );
- FlushVol(0L, vnum);
- }
-